package kotlinquanweizhinan.chapt06.exception

class SwordJuggler {


}

fun main() {
    var swordsJuggling:Int?=null
    val isJugglingProficient = (2..3).shuffled().last() == 3
    println(isJugglingProficient)
    if(isJugglingProficient){
        swordsJuggling = 1
    }

    /**
     * 对于一个可空变量，使用!!.操作符很危险。你有1/3的概率成为熟手，多抛一把剑。
     * 还有2/3的概率，应用程序会崩溃
     */
    try {
        perficiencyCheck(swordsJuggling)
        swordsJuggling = swordsJuggling!!.plus(1)
        juggleSwords(swordsJuggling)
    }catch (e:java.lang.Exception){
        println(e)
    }

    println("You juggle $swordsJuggling swords!")
}

fun perficiencyCheck(swordsJuggling:Int?){
    //swordsJuggling ?: throw IllegalStateException("Player cannot juggle swords")
    //swordsJuggling ?: throw  UnskilledSwordJugglerException()

    //使用先决条件函数抛出异常
    checkNotNull(swordsJuggling,{"Plalyer cannot juggle swords"})
}

class UnskilledSwordJugglerException():
        IllegalStateException("Player cannot juggle swords")

fun juggleSwords(swordsJuggling:Int){
    require(swordsJuggling >=3,{"Juggle at least 3 swords to be exciting."})
}